*Count by treatment arms

cap log close

global path = "****"
global path2 = "****"

local name= "sum_stats" 
local date = "062420"

clear
set more off
set matsize 1000
cd "$path"
log using "$logname", replace

use "$path/Data/sum_stats_data_prepcode.dta",clear 

				
* Create necessary variables 
gen overall = 1
gen control = (treatment == 0)
bysort tin: gen hh_count = 1 if _n == 1


gen arm = 1 	if base == 1 //base
replace arm = 2 if early == 1 //early
replace arm = 3 if nonpersonalize == 1 //personalized info
replace arm = 4 if exemption_info == 1 //exemption info

cap drop arm1 arm2 arm3 arm4

*tab arm, gen(arm)
forval x = 1/4 {
	gen arm`x' = arm==`x'
}

gen english = (spanish == 0)
replace english = 0 if control == 1 
replace spanish = 0 if control == 1

mat drop _all
estimates clear
eststo clear

cap program drop counts_ind
cap program drop counts_hh
cap program drop frac_hh

*program for column 1
program counts_ind, eclass
	 syntax varlist 
	 marksample touse
     markout `touse' `by'
     tempname ind  
	 foreach var of local varlist {
       sum `var'
		mat `ind' = nullmat(`ind'), r(sum)
     }
	ereturn post `ind'
end

*program for column 2
program counts_hh, eclass
	 syntax varlist 
	 marksample touse
     markout `touse' `by'
     tempname hh_count 
	 foreach var of local varlist {
		sum `var' if hh_count == 1
			mat `hh_count' = nullmat(`hh_count'), r(sum)
     }
	ereturn post `hh_count'
end

*program for column 3
program frac_hh, eclass
	 syntax varlist 
	 marksample touse
     markout `touse' `by'
     tempname ind hh_count hh_frac 
	 foreach var of local varlist {
 		 sum `var' if hh_count == 1
			mat `hh_frac' = nullmat(`hh_frac'), r(mean)
     }
	 ereturn post `hh_frac'
end

	
local groups overall treat control arm1 arm2 arm3 arm4 english spanish

*calculate and save estimates

counts_ind `groups'
eststo
estimates save "$path/`name'_counts_ind_`date'.ster", replace 
counts_hh `groups'
eststo
estimates save "$path/`name'_counts_hh_`date'.ster", replace 
frac_hh `groups'
eststo
estimates save "$path/`name'_frac_hh_`date'.ster", replace 

log close


